#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
#include <sys/stat.h>
#include <vector>

struct Fileoperator
{
    static std::string readfile(const std::string filepath){
        std::ifstream file(filepath,std::ios::binary);
        if(!file.is_open()) {
            std::cerr<<"打开文件失败！"<<filepath<<std::endl;
        }
        //将数据读入vector<char>中保留了每个字节;
        ssize_t size = getfilesize(filepath);
        std::string content(size,'\0');
        file.read(&content[0],size);
        return content;
    }
    static std::string getExtension(const std::string& path) {
        auto pos = path.rfind('.');
        std::string ret;
        if(pos != std::string::npos){
            ret = path.substr(pos);
        }
        return  ret;
    }
    static bool is_existent(const std::string& filePath){
        struct stat filesinfo;
    
        if(stat(filePath.c_str(),&filesinfo) == 0) return true;
        else return false;
    }
    static off_t getfilesize(const std::string& filePath){
        struct stat filesinfo;
        if (stat(filePath.c_str(), &filesinfo) == -1) {
            std::cerr << "Failed to get file size: " << filePath << std::endl;  // 输出错误信息
            return 0;
        }
        return filesinfo.st_size;
    }
};
